package com.usian.student.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.common.pojo.ResultVo;
import com.usian.student.dao.StudentDao;
import com.usian.student.feign.ClassFeignService;
import com.usian.student.feign.LoginFeignService;
import com.usian.student.pojo.TbClass;
import com.usian.student.pojo.TbStudent;
import com.usian.student.pojo.UserTo;
import com.usian.student.service.StudentService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class StudentServiceImpl extends ServiceImpl<StudentDao, TbStudent> implements StudentService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private StudentDao studentDao;
    //调用feign
    @Autowired
    private ClassFeignService classFeignService;

    @Autowired
    private LoginFeignService loginFeignService;

//@Transactional
    @Override
    public ResultVo register(TbStudent tbStudent) throws Exception {


//        VerifyReaIName verifyReaIName = new VerifyReaIName();
//        Map result = verifyReaIName.checkUser(tbStudent.getCardNo(), tbStudent.getPhone(), tbStudent.getName());
//        System.out.println(result);

//         正确的返回方法
        if (true) {
            //       请求的接口成功
            if (true) {

//                    判断手机号是否正确  判断用户输入的身份验证码是否

//                    身份验证redis验证码是否过期
//                    验证用户输入的验证码和redis的验证码是否一致

                String redisCode = stringRedisTemplate.opsForValue().get(tbStudent.getPhone());
                if (!StrUtil.isEmpty(redisCode)) {
                    if (redisCode.equals(tbStudent.getCode())) {
//                        验证是否重复
                        LambdaQueryWrapper<TbStudent> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                        lambdaQueryWrapper.eq(TbStudent::getPhone, tbStudent.getPhone());
                        long count = studentDao.selectCount(lambdaQueryWrapper);
                        if (count > 0) {
                            //改手机号已注册
                            return new ResultVo(300, "该用户已注册  不能重复注册", null);
                        } else {
                            //需要根据班级id获取对应的项目经理id和班主任id
                            ResultVo resultVo = classFeignService.info(tbStudent.getClassId());
//                            TbClass tbClass = (TbClass) resultVo.getData();
                            TbClass tbClass =JSON.parseObject(JSON.toJSONString(resultVo.getData()),TbClass.class);
                            tbStudent.setHid(tbClass.getHid());
                            tbStudent.setHid(tbClass.getHid());
//                                 用户未注册，可以直接保存到数据库
                            studentDao.insert(tbStudent);

//                            调用login服务 tb_user登录表中插入数据
                            UserTo userTo = new UserTo(tbStudent.getName(),tbStudent.getPhone(),5);
                            loginFeignService.save(userTo);


                            return new ResultVo(200, "success", null);
                        }
                    } else {

                        return new ResultVo(300, "验证码输入错误", null);
                    }
                } else {
                    return new ResultVo(300, "验证码过期", null);
                }
            } else {
                return new ResultVo(300,"异常", null);
//                return new ResultVo(Integer.parseInt(result.get("code") + ""), result.get("msg") + "", null);
            }
        } else {
            //  请求的接口异常
            return new ResultVo(300,"异常", null);
//            return new ResultVo(Integer.parseInt(result.get("code") + ""), result.get("msg") + "", null);
        }

    }



}
